'use strict';

angular.module('afasApp')
  .controller('SexesCtrl', ['$scope', 'Services', 'Sexes', '$state', function ($scope, Services, Sexes, $state) {

    $scope.sexes = [];
    $scope.sexNew = {};

    function applyRemoteData( newData ) {
      $scope.sexes = newData;
    }

    function loadRemoteData() {
      Services.getItems('sexes')
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.addSex = function () {
      Services.add('sexes', $scope.sexNew)
      .then(function(){
        $scope.sexNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.deleteSex = function (sex) {
      Services.delete('sexes', sex)
      .then(function(){
        loadRemoteData();
      });
    };
  }]).controller('SexCtrl', ['$scope', 'Services', 'Sexes', '$state', '$stateParams', function ($scope, Services, Sexes, $state, $stateParams) {

    $scope.currentSex = {};
    $scope.currentID = $stateParams.id;
    // $scope.currentSex =Sexes.getSex($stateParams.id);

    Services.getItem('sexes', $scope.currentID)
    .then(function(results){
      $scope.currentSex = results.data;
    });

    $scope.updateSex = function () {
      Services.update('sexes', $scope.currentSex, $scope.currentID)
      .then(function(){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };
  }]);
